#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

DATE="`date +%Y%m%d_%H%M%S`"
HASH="`git rev-parse --short HEAD`"
SUFFIX="nightly-$HASH-$DATE"

V="`xmlstarlet sel -t -m /_:project/_:version -v  . pom.xml`"
NEW_HIVE="${NEW_HIVE:-${V/-*}-$SUFFIX}"
V="`xmlstarlet sel -t -m /_:project/_:version -v  . storage-api/pom.xml`"
NEW_SA="${NEW_SA:-${V/-*}-$SUFFIX}"
V="`xmlstarlet sel -t -m /_:project/_:version -v  . standalone-metastore/pom.xml`"
NEW_MS="${NEW_MS:-${V/-*}-$SUFFIX}"


mvn_versions_set="mvn versions:set versions:commit -DgenerateBackupPoms=false"

$mvn_versions_set -B -DnewVersion=$NEW_HIVE
$mvn_versions_set -B -DnewVersion=$NEW_SA -pl storage-api
$mvn_versions_set -B -DnewVersion=$NEW_MS -pl standalone-metastore

xmlstarlet edit -L -P --update "/_:project/_:properties/_:hive.version" \
  --value $NEW_HIVE standalone-metastore/pom.xml

xmlstarlet edit -L -P --update "/_:project/_:properties/_:storage-api.version" \
  --value $NEW_SA pom.xml standalone-metastore/pom.xml

xmlstarlet edit -L -P --update "/_:project/_:properties/_:standalone-metastore.version" \
  --value $NEW_MS pom.xml

